local skel = fk.CreateSkill {
  name = "efengqi__weimu",
}

Fk:loadTranslationTable{
  ["efengqi__weimu"] = "帷幕",
  [":efengqi__weimu"] = "你于回合内伤害时，你可以防止之并摸等量张牌；你于回合外受到伤害时，你可将一张锦囊牌当【闪电】使用，若如此做，防止此伤害。",

  ["#efengqi__weimu-use"] = "帷幕:你可将一张锦囊牌当【闪电】使用，防止你受到的伤害",
  ["#efengqi__weimu-draw"] = "帷幕:你可防止你受到的伤害并摸等量张牌",

  ["$efengqi__weimu1"] = "此计伤不到我。",
  ["$efengqi__weimu2"] = "你奈我何？",
}

skel:addEffect(fk.DamageInflicted, {
  anim_type = "defensive",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(skel.name) then
      if player.phase == Player.NotActive then
        return not player:isKongcheng() and not player:hasDelayedTrick("lightning")
        and not table.contains(player.sealedSlots, Player.JudgeSlot)
      else
        return true
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    if player.phase ~= Player.NotActive then return player.room:askToSkillInvoke(player,
    { skill_name = skel.name, prompt = "#efengqi__weimu-draw"}) end
    local ids = table.filter(player:getCardIds("h"), function (id)
      if Fk:getCardById(id).type == Card.TypeTrick then
        local c = Fk:cloneCard("lightning")
        c.skillName = skel.name
        c:addSubcard(id)
        return player:canUseTo(c, player)
      end
    end)
    ids = player.room:askToCards(player, {
      min_num = 1, max_num = 1, skill_name = skel.name, pattern = tostring(Exppattern{ id = ids }), prompt = "#efengqi__weimu-use"
    })
    if #ids > 0 then
      event:setCostData(self, ids[1])
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    if player.phase ~= Player.NotActive then
      player:drawCards(data.damage, skel.name)
    else
      local c = Fk:cloneCard("lightning")
      c.skillName = skel.name
      c:addSubcard(event:getCostData(self))
      player.room:useCard{from = player, tos = {player}, card = c}
    end
    data:preventDamage()
  end,
})

return skel
